package com.aoip.telemetry.telemetry.datastorage;

import com.alibaba.fastjson.JSON;
import com.aoip.telemetry.telemetry.datastorage.service.ElementSaveService;
import com.aoip.telemetry.telemetry.entity.common.LogEntity;
import com.aoip.telemetry.telemetry.entity.common.TableDataEntity;
import com.aoip.telemetry.telemetry.entity.enums.LogLevelEnum;
import com.aoip.telemetry.telemetry.entity.enums.LogTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据入库
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/1/5 15:10
 */
@Component
public class ElementDataSave {

    @Resource
    private ElementSaveService elementSaveService;

    /**
     * 插入数据格式
     * [
     * {tableName=WR_MP_Z_R, stCode=2202830004001, fieldMap={tm=2021-11-09T10:00:57, mp_cd=2202830004001, MP_Z=10.000}},
     * {tableName=WR_MP_Z_R, stCode=2202830004002, fieldMap={tm=2021-11-09T10:00:57, mp_cd=2202830004002, MP_Z=15.000}},
     * {tableName=ws_work_condition, st_cd=2202830400, fieldMap={tm=2021-11-09T10:00:57, terminal_status=0101000100000000, alarm_info=1000000100100000, st_cd=2202830400}}
     * ]
     *
     * @author chenbai
     * @date 2021/11/16
     **/
    public int saveData(List<TableDataEntity> list) {
        Integer successSaveNum = elementSaveService.saveElementData(list);
        LogEntity entity = new LogEntity(LogTypeEnum.LOG_ELEMENT_SAVE);
        entity.setOriginalText(JSON.toJSONString(list));
//        if (successSaveNum != list.size()) {
        if (successSaveNum == -1) {
            entity.setLoggerLevel(LogLevelEnum.ERROR);
            entity.setLogMsg("数据入库失败。");
        }
        return successSaveNum;
    }


}
